package com.cwn.wethink.common.utils.rar;

import com.cwn.wethink.common.utils.rar.ExtractCallback;
import net.sf.sevenzipjbinding.*;
import net.sf.sevenzipjbinding.impl.RandomAccessFileInStream;

import java.io.File;
import java.io.RandomAccessFile;
import java.util.List;


public class SevenZipDecompression {
    public static void main(String[] args) {
        unRar(new File("rarFIle"),"dir");
    }
    /**
     * 解压rar文件
     * @param rarFile rar文件
     * @param dstDirectoryPath 解压路径
     * @return 解压后的文件名列表
     */
    public static List<String> unRar(File rarFile, String dstDirectoryPath){
        IInArchive archive;
        RandomAccessFile randomAccessFile;
        try {
            randomAccessFile = new RandomAccessFile(rarFile,"r");
            archive = SevenZip.openInArchive(null,new RandomAccessFileInStream(randomAccessFile));
            int[] in = new int[archive.getNumberOfItems()];
            for (int i = 0; i < in.length; i++) {
                in[i] = i;
            }
            final ExtractCallback extractCallback = new ExtractCallback(archive, dstDirectoryPath);
            archive.extract(in,false,extractCallback);
            archive.close();
            randomAccessFile.close();
            return extractCallback.getNameList();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

}